package com.ninth_group.utils;

import com.ninth_group.po.CityInfo;

import java.util.*;


public class CityInfoUtil {
    private static RedisSerializerUtil redisUtil;

    public static void setRedisUtil(RedisSerializerUtil redisUtil) {
        CityInfoUtil.redisUtil = redisUtil;
    }


    /**
     * 获取城市编码、省份以及城市名称, 便于后续查找
     * @return 城市编码、省份以及城市名称
     */
    public static Set<CityInfo> getCityDataIncludingProvince(HiveUtil hiveUtil) {
        // 假设你已经在某个地方创建了 HiveUtil 的实例 hiveUtil
        String sql = "SELECT city_id, province, city FROM city_base_info";

        Set<CityInfo> cityInfoset = new HashSet<>();

        List<Map<String, Object>> cityList = hiveUtil.queryForMap(sql);
        for (Map<String, Object> cityMap : cityList) {
            String cityId = (String) cityMap.get("city_id");
            String province = (String) cityMap.get("province");
            String city = (String) cityMap.get("city");
            cityInfoset.add(new CityInfo(cityId, province, city));
        }
        return cityInfoset;
    }

    /**
     * 获取所有城市id
     * @param provinceName 省份名称
     * @param cityName 城市名称
     * @param hiveUtil 工具类
     * @return 所有城市id
     */
    public static List<String> getCityIds(Set<CityInfo> cityInfo, String provinceName,
                                          String cityName, HiveUtil hiveUtil) {
        if (cityInfo.isEmpty())
            return null;

        List<String> matchedCityIds = cityInfo.stream()
                .filter(ci -> ci.getProvince()
                        .equals(provinceName) && ci.getCity().equals(cityName))
                .map(CityInfo::getCityId)
                .toList();

        if (matchedCityIds.isEmpty())
            return null;

        return matchedCityIds;
    }

    // 获取所有城市信息
    public static Set<CityInfo> getCityInfo(HiveUtil hiveUtil) {
         return CityInfoUtil.getCityDataIncludingProvince(hiveUtil);
    }
}
